﻿using Common.Library.Files;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Text;
using System.Threading.Tasks;

namespace Common.Library.StringExt.Randoms
{
    public class RandomAddr
    {
        static List<Province> list;
        static RandomAddr()
        {
            //物理文件地址
            string fileJson = ServerAddress.GetApplicationBase() + "/DLL/1.json";
            //获取到json数据
            string json = FileStreamhelper.ReadFile(fileJson);
            //反序列化得到省市县区域地址
            list = JsonConvert.DeserializeObject<List<Province>>(json);
            //end 初始化完成
        }

        public static string Random()
        {
            string ProvinceName = "";
            string CityName = "";
            string CountyName = "";
            string StreetName = "";
            int provinceCount = list.Count();
            int randomIndex = RandomExt.GetNumber(0, provinceCount);
            Province provinceEntity = list[randomIndex];
            ProvinceName = provinceEntity.ProvinceName;

            int cityCount = provinceEntity.CityList.Count;
            randomIndex = RandomExt.GetNumber(0, cityCount);
            City cityEntity = provinceEntity.CityList[randomIndex];
            CityName = cityEntity.CityName;

            int countyCount = cityEntity.CountyList.Count;
            randomIndex = RandomExt.GetNumber(0, countyCount);
            County countyEntity = null;
            if (randomIndex != 0)
            {
                countyEntity = cityEntity.CountyList[randomIndex];
                CountyName = countyEntity.CountyName;
            }

            if (countyEntity != null)
            {
                int streetCount = countyEntity.StreetList.Count;
                randomIndex = RandomExt.GetNumber(0, streetCount);
                if (randomIndex != 0)
                {
                    Street streetEntity = countyEntity.StreetList[randomIndex];
                    StreetName = streetEntity.StreetName;
                }
            }
           
           
            return $"{ ProvinceName } { CityName } { CountyName } { StreetName }";
        }



    }
    public class Province
    {
        public string ProvinceName { get; set; }
        public List<City> CityList { get; set; }
    }

    public class City
    {
        public string CityName { get; set; }
        public List<County> CountyList { get; set; }
    }


    public class County
    {
        public string CountyName { get; set; }
        public List<Street> StreetList { get; set; }
    }

    public class Street
    {
        public string StreetName { get; set; }
    }

    public class Area
    {
        public string AreaName { get; set; }
    }

}
